<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/22 0022
 * Time: 上午 8:41
 */
header('Content-Type:text/html;charset=utf-8');
$link=mysqli_connect('localhost','root','') or die('数据库连接失败！');
mysqli_query($link,'set names utf8');
mysqli_query($link,'use `itcast`') or die('itcast数据库不存在！');
$city = array('北京','上海','广州','其他');
$skill = array('HTML','JavaScript','PHP','C++');
$id = 1;
if(!empty($_POST)){
    var_dump($_POST);
    $fields = array('nickname','gender','email','qq','url','city','skill','description');
    foreach($fields as $v){
        $save_data[$v] = isset($_POST[$v]) ? $_POST[$v] : '';
    }
    if($save_data['gender']!='男' && $save_data['gender']!='女'){
        die('保存失败：未选择性别。');
    }
    if($save_data['city']!='未选择' && !in_array($save_data['city'],$city)){  
        die('保存失败：您填写的城市不在允许的城市列表中。');
    }
    if(is_array($save_data['skill'])){
        $save_data['skill'] = array_intersect($skill,$save_data['skill']);	
        $save_data['skill'] = implode(',',$save_data['skill']); 
    }else{
        $save_data['skill'] = '';
    }
    $sql = 'update `userinfo` set ';
    foreach($save_data as $k=>$v){
        $sql .= "`$k`='".mysqli_real_escape_string($link,$v)."',"; 
    }
    $sql = rtrim($sql,',')." where id=$id"; 
    $rst = mysqli_query($link,$sql);
    echo $rst ? "保存成功：$sql" : "保存失败：$sql<br>".mysqli_error();
}
$sql = "select `nickname`,`gender`,`email`,`qq`,`url`,`city`,`skill`,`description` from `userinfo` where `id`=$id";
$rst = mysqli_query($link,$sql);

if(!$rst) die(mysqli_error($link));
$data = mysqli_fetch_assoc($rst);
if(!$data){
    die('没有找到ID为'.$id.'的用户信息！');
}
$data['skill'] = explode(',',$data['skill']);
define('APP','itcast');
require 'profile_html.php';